﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClientWorkflowLibrary;
using System.Threading;
using ClientRenderSA.DTDBService;
using System.Configuration;

namespace ClientRenderSA
{
    /// <summary>
    /// Stand alone rendering app - just sits at a command line and tries to run over and over and over again.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            RenderATalk();
        }

        static Mutex output = new Mutex();

        static RenderWFManager _mgr;

        public static void RenderATalk()
        {
            ///
            /// Great. Now we can actually start the thing up
            /// 

            StartRenderMgr();

            ///
            /// Every 10 seconds grab the list of talks that aren't rendered
            /// and print something out...
            /// 

            int nUnrenderedTalks = -1;
            string lastTitle = "";

            while (true)
            {
                try
                {
                    using (DTDBClient dcl = new DTDBClient())
                    {
                        BasicTalkInfo[] talks = dcl.GetUnrenderedTalks(10);

                        if (nUnrenderedTalks != talks.Length)
                        {
                            nUnrenderedTalks = talks.Length;
                            lock (output)
                            {
                                Console.WriteLine(" -> Number of unrendered talks is now " + nUnrenderedTalks);
                            }
                        }

                        if (nUnrenderedTalks > 0)
                        {
                            if (lastTitle != talks[0].Title)
                            {
                                lock (output)
                                {
                                    Console.WriteLine(" -> First talk title change: " + talks[0].Title);
                                }
                                lastTitle = talks[0].Title;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    lock (output)
                    {
                        Console.WriteLine("Failed to access the database client! Try again soon");
                    }
                }
                Thread.Sleep(10 * 1000);
            }
        }

        /// <summary>
        /// Setup the render manager!
        /// </summary>
        private static void StartRenderMgr()
        {
            if (_mgr != null)
            {
                _mgr.Stop();
                _mgr = null;
                Console.WriteLine("Stopping the render manager");
            }
            _mgr = new RenderWFManager();

            ///
            /// Configure the manager
            /// 

            _mgr.TempRenderDir = ConfigurationSettings.AppSettings["TempRenderingDir"];
            _mgr.TalkOutputDir = ConfigurationSettings.AppSettings["TalkOutputDir"];
            _mgr.DPI = Convert.ToInt32 (ConfigurationSettings.AppSettings["DPI"]);
            _mgr.ImageQuality = Convert.ToDouble(ConfigurationSettings.AppSettings["ImageQuality"]);

            ///
            /// Link to callbacks and get it running
            /// 

            _mgr.WorkflowFinished += new RenderWFManager.WorkflowFinishedCallback(mgr_WorkflowFinished);
            _mgr.Start();
            Console.WriteLine("Started the render manager");
        }

        /// <summary>
        /// Workflow managed to finish up - print something out!
        /// </summary>
        /// <param name="e"></param>
        static void mgr_WorkflowFinished(Exception e)
        {
            if (e == null)
            {
                lock (output)
                {
                    Console.WriteLine("Finished meeting with no errors");
                }
            }
            else
            {
                lock (output)
                {
                    Console.WriteLine("Finished with an error: " + e.Message);
                    Console.WriteLine(e.StackTrace);
                }
            }
        }
    }
}
